package com.tinshow.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tinshow.config.cookie.CookieUtil;
import com.tinshow.config.utils.ImageUtil;
import com.tinshow.config.utils.MailSenderInfo;
import com.tinshow.config.utils.MailUtil;
import com.tinshow.config.utils.RelativeDateFormat;
import com.tinshow.model.article.Article;
import com.tinshow.model.article.Music;
import com.tinshow.model.article.vo.ArticleVo;
import com.tinshow.model.index.Navigation;
import com.tinshow.model.user.User;
import com.tinshow.service.index.ArticleService;
import com.tinshow.service.index.MusicService;
import com.tinshow.service.index.NavigationService;
import com.tinshow.web.httpclient.EachClient;
import freemarker.template.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {

    private static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private NavigationService navigationService;
	@Autowired
	private ArticleService articleService;
	@Autowired
    private MusicService musicService;
	@Autowired
    private JavaMailSender javaMailSender;

	@RequestMapping("/")
    @Cacheable("index")
	public ModelAndView index(Model model, HttpServletRequest request, HttpServletResponse response){
        logger.info("=============welcome to tinshow===============");
//	    List<Music> musicList= musicService.selectAllMusic();
//        JSONArray jsonArray = (JSONArray) JSON.toJSON(musicList);
//        String jsons = jsonArray.toJSONString();
//        model.addAttribute("musics",jsons);
        HttpSession session = request.getSession();
//        System.out.println(request.changeSessionId());
        return new ModelAndView("index").addObject(model);
	}

	@RequestMapping("/get_article_index")
    @ResponseBody
    public ModelAndView getArticleIndex(Model model){
        List<ArticleVo> article_list = articleService.getArticleForIndex();
        for (ArticleVo articleVo : article_list) {
            articleVo.setDateText(RelativeDateFormat.format(articleVo.getDtCreated()));
        }
        model.addAttribute("article",article_list);
	    return new ModelAndView("topic/article_index");
    }

	@RequestMapping("/get_echo_hot")
	public String getEcho4Hot(){
        List<Music> list = EachClient.getEchoIndex();
        for (int i = 0; i < list.size(); i++) {
            Music music = musicService.getMusicForUrl(list.get(i).getUrl());
            if(music == null){
                musicService.insertMusic(list.get(i));
            }else{
                continue;
            }
        }
        return "success";
    }

    @RequestMapping("/search")
    public ModelAndView search(){
	    return new ModelAndView("search");
    }

	@RequestMapping("/index")
    public ModelAndView home(){
	    return new ModelAndView("redirect:/");
    }
}
